﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_IcmPoSeq : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        IBLL_ICM_PurchaseOrder po = new B_ICM_PurchaseOrder();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public void SetComboBoxEnable(bool isEnable)
        {
            RadComboBox ddl = ddlIcmPoSeq;
            ddl.Enabled = isEnable;
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlIcmPoSeq;
            return ddl;
        }

        public void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            string strBgmYearSeq = (lblBgmYear.Text.Length > 0 ? lblBgmYear.Text : null);

            IList<M_ICM_PurchaseOrder> infolist = po.GetList(strBgmYearSeq, strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlIcmPoSeq.Items.Add(new RadComboBoxItem(infolist[i].icm_po_name, infolist[i].icm_po_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset, string strBgmYearSeq)
        {
            if (nWidth != null)
                ddlIcmPoSeq.Width = Unit.Pixel(nWidth.Value);

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblBgmYear.Text = strBgmYearSeq;
        }

        public void SetSelectedValue(string strValue)
        {
            if (strValue.Length > 0)
            {
                lblIcmPoSeq.Text = strValue;

                if (ddlIcmPoSeq.FindItemByValue(strValue) == null)
                {
                    M_ICM_PurchaseOrder info = po.GetInfo(strValue);
                    ddlIcmPoSeq.Text = info.icm_po_name;
                }
                else
                {
                    ddlIcmPoSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlIcmPoSeq.SelectedIndex = -1;
                ddlIcmPoSeq.Text = "";
                lblIcmPoSeq.Text = "";
            }
        }

        public string GetSelectedValue()
        {
            string strValue = lblIcmPoSeq.Text;

            return strValue;
        }

        public void ddlIcmPoSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strValue = ddlIcmPoSeq.SelectedValue;
            lblIcmPoSeq.Text = strValue;

            if (Child != null)
            {
                Child.Text = "";

                if (strValue.Length > 0)
                {
                    switch (Child.ID)
                    {
                        case "ddlContactSeq" :
                            string ID = this.ID;
                            M_ICM_PurchaseOrder PoInfo = po.GetInfo(strValue);
                            App_UI.UserControls.DropDownList_Contact uc = (App_UI.UserControls.DropDownList_Contact)Child.Parent;
                            uc.SetSelectedValue(PoInfo.icm_con_seq.ToString());

                            //((RadNumericTextBox)Parent.FindControl("txtAmount")).Text = PoInfo.document_amount.ToString();
                            //((RadNumericTextBox)Parent.FindControl("txtBathAmount")).Text = PoInfo.document_amount.ToString();
                            //switch (ID)
                            //{
                            //    case "DropDownList_IcmPoSeqContract":
                            //        //uc.LoadControl(null, null, "Contract", strValue);

                            //        break;
                            //    case "DropDownList_IcmPoSeqPrepay":
                            //        //uc.LoadControl(null, null, "Prepay", strValue);
                            //        break;
                            //}

                            break;
                    }
                }
                else
                {
                    Child.DataSource = null;
                    Child.DataBind();
                }
            }
        }

        protected void ddlIcmPoSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }
    }
}